package com.hunzi.controller.admin;

import com.hunzi.domain.User;
import com.hunzi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Arrays;
import java.util.List;

//因为Class类名要唯一 ,所有要给Controller重新取一个名字
@Controller("admin_UserController")
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 1.显示所有用户列表
    @RequestMapping("/list")
    public String list(Model model){
        List<User> users = userService.selectAll ();
        model.addAttribute ( "users" ,users );
        return "admin/user-list";
    }


    //2.跳转到添加用户的界面
    @RequestMapping("/addUI")
    public String addUI(){

        return "admin/user-add";
    }
    //2.添加用户
    @RequestMapping("/add")
    public String add(User user){
        userService.insert ( user );
        return "redirect:/admin/user/list";
    }
    //3.删除用户
    @RequestMapping("/delete")
    public String delete(int uid){
        userService.deleteByPrimaryKey ( uid );
        return "redirect:/admin/user/list";
    }

    //4.修改用户-回显数据(根据id查询)
    //uid直接获取用户传递过来的uid
    @RequestMapping("/updateUI")
    public String updateUI( int uid , Model model){
        User user = userService.selectByPrimaryKey ( uid );
        model.addAttribute ( "user" ,user );
        return "admin/user-update";
    }
    //修改用户
    @RequestMapping("/update")
    public String update(User user){
        userService.updateByPrimaryKey ( user );
        return "redirect:/admin/user/list";
    }

    /*5.多组合查询*/
    @RequestMapping("/listAll")
    public String selectByWhere( User user ,Model model ){
        /*调用service中的组合查询方法*/
        List<User> users = userService.selectByWhere ( user );
        model.addAttribute ( "users" , users );
        return "admin/user-list" ;

    }

    /*6.多组合查询*/
    @RequestMapping("/deleteBatch")
    public String deleteByIds( int[] ids){
        System.out.println ( Arrays.toString ( ids ) );
        userService.deleteByIds ( ids );
        return "redirect:/admin/user/list";
    }


}
